// pages/product/product.js
import {Product} from 'product-model';
var product = new Product();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden:false,
        hiddenSmallImg:true,
        loadingHidden:false,
        countsArray:[1,2,3,4,5,6,7,8,9],
        productCounts:1,
        currentTabsIndex:0,
        cartTotalCounts:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.id = options.id;
        this._onload();
    },
    _onload:function(callback){
        var that = this;
        product.getDatailInfo(this.data.id,res=>{
            that.setData({
                loadingHidden:true,
                product:res
            })
            callback && callback();
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this._onload(()=>{
            wx.stopPullDownRefresh();
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    //选择购买数目
    bindPickerChange:function(e){
      this.setData({
          productCounts: this.data.countsArray[e.detail.value],
      })
    },
    //切换详情面板
    onTabsItemTap:function(event){
      var index = product.getDataSet(event,'index');
      this.setData({
          currentTabsIndex:index
      })
    },
    //添加到购物车
    onAddingToCartTap:function(event){
      // 防止快速点击
        if (this.data.isFly){
          return;
        }
        this._flyToCartEffect(event);
        this.addToCart();
    },
    onCartTap:function(){
        wx.switchTab({
            url: '/page/cart/cart',
        })
    },
    addToCart:function(){
      var temObj = {},keys=['id','name','main_img_url','price'];
      for (var key in this.data.product){
          // if(keys.indexOf(key)>=0){
          //     tempObj[key]=this.data.product[key];
          // }
      }
    },
    /*加入购物车动效*/
    _flyToCartEffect:function(events){
        //获得当前点击的位置，距离可视区域左上角
        var touches=events.touches[0];
        var diff={
                x:'25px',
                y:25-touches.clientY+'px'
            },
            style='display: block;-webkit-transform:translate('+diff.x+','+diff.y+') rotate(350deg) scale(0)';  //移动距离
        this.setData({
            isFly:true,
            translateStyle:style
        });
        var that=this;
        setTimeout(()=>{
            that.setData({
                isFly:false,
                translateStyle:'-webkit-transform: none;',  //恢复到最初状态
                isShake:true,
            });
            setTimeout(()=>{
                var counts=that.data.cartTotalCounts+that.data.productCounts;
                that.setData({
                    isShake:false,
                    cartTotalCounts:counts
                });
            },200);
        },1000);
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '零食商贩 Pretty Vendor',
            path: 'pages/product/product?id=' + this.data.id
        }
    }
})